package com.qf;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsCategory;
import com.qf.service.CategoryService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品类目
 * @author sym
 * @version V1.0
 * @Project java2203
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/6/21 19:21
 */
//解决前后端跨域访问问题
    @CrossOrigin("*")
    @RestController
    @RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    @GetMapping("/list")
    public  Object list(Integer id,String name,
                        @RequestParam(defaultValue = "1") Integer page,
                        @RequestParam(defaultValue = "20") Integer limit,
                        @RequestParam(defaultValue = "add_time") String sort,
                        @RequestParam(defaultValue = "desc") String order){


        //查询并返回分页对象
        IPage<DtsCategory> resultPage = categoryService.queryCategory(id, name, page, limit, sort, order);

        //获取结果集
        List<DtsCategory> list = resultPage.getRecords();

        //获取查询总条数
        long total = resultPage.getTotal();

        //封装对象返回
        Map<String,Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", list);
        return ResponseUtil.ok(data);
    }
    /**
     *添加
     */
    @PostMapping("/create")
    public  Object add(@RequestBody DtsCategory category){

        categoryService.add(category);
        return  ResponseUtil.ok();
    }


    /**
     * 修改
     * @param
     * @return
     */
    @PostMapping("/update")
    public Object update(@RequestBody DtsCategory category){
        int count = categoryService.update(category);
        if (count == 0) {
            return ResponseUtil.fail();
        }
        return ResponseUtil.ok();
    }

    /**
     * 删除
     * @return
     */
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsCategory category) {
        int count = categoryService.delete(category);
        if (count == 0) {
            return ResponseUtil.fail();
        }
        return ResponseUtil.ok(category);
    }

    /**
     * 下拉
     */
    @RequestMapping("/l1")
    public  Object  l1(){
      List<CatVo> count = categoryService.findCataVoCat();
      return  ResponseUtil.ok(count);
    }
}
